package com.changpay.zywy.ui.activity.loan

import android.support.v7.widget.LinearLayoutManager
import android.view.View
import com.chad.library.adapter.base.BaseQuickAdapter
import com.changpay.zywy.R
import com.changpay.zywy.adapter.LoanHistoryAdapter
import com.changpay.zywy.app.App
import com.changpay.zywy.base.BaseActivity
import com.changpay.zywy.base.IView
import com.changpay.zywy.bean.BillDetailBean
import com.changpay.zywy.ext.showToast
import com.changpay.zywy.ext.sss
import com.changpay.zywy.http.RetrofitHelper
import com.changpay.zywy.ui.dialog.SelectMonthDialog
import com.changpay.zywy.utils.DateUtil
import com.changpay.zywy.wight.SpaceItemDecoration
import kotlinx.android.synthetic.main.activity_loan_history.*
import kotlinx.android.synthetic.main.activity_loan_history.recyclerView
import kotlinx.android.synthetic.main.title_layout.*

class LoanHistoryActivity : BaseActivity(), IView {
    private var endTime = ""

    override fun showLoading() {

    }

    override fun hideLoading() {

    }

    override fun showDefaultMsg(msg: String) {
        showToast(msg)
    }

    override fun showMsg(msg: String) {
        showToast(msg)
    }

    override fun showError(errorMsg: String) {
        showToast(errorMsg)
    }


    private val onClickListener = View.OnClickListener { view ->
        when (view.id) {
            R.id.iv_back -> finish()
        }
    }


    override fun attachLayoutRes(): Int {
        return R.layout.activity_loan_history
    }

    override fun initData() {

    }

    override fun initView() {
        tv_title.text = "历史账单"
        iv_back.setOnClickListener(onClickListener)
        tv_submit.setOnClickListener(onClickListener)
        tv_submit.setOnClickListener {
            val builder = SelectMonthDialog.Builder(this)
            builder.setOnDialogClickListener { v, month ->

                endTime = month.substring(0, 4) + month.substring(4, 6)
                tv_show_time.text = month.substring(0, 4) + "-"+ month.substring(4, 6)
                loadData()

            }
            val build = builder.build<SelectMonthDialog>()
            build.isCancelable = true
            build.show(supportFragmentManager)

        }
        initRecyclerView()
    }

    override fun start() {

        loadData()
    }

    private fun loadData() {
        val param = hashMapOf<String, String>()
        param["key"] = token
        param["allot_id"] = intent.getStringExtra("allot_id")
        param["month"] = endTime
        // 加载底部数据
        RetrofitHelper.service.get_tBill_detail_list(param).sss(this) {
            datas.clear()
            datas.addAll(it.result)
            mAdapter.notifyDataSetChanged()
        }

    }


    /**
     * datas
     */
    private val datas = mutableListOf<BillDetailBean>()

    /**
     * RecyclerView Divider
     */
    private val recyclerViewItemDecoration by lazy {
        this.let {
            SpaceItemDecoration(App.context!!)
        }
    }

    /**
     * Adapter
     */
    private val mAdapter: LoanHistoryAdapter by lazy {
        LoanHistoryAdapter(App.context, datas)
    }


    /**
     * LinearLayoutManager
     */
    private val LayoutManager: LinearLayoutManager by lazy {
        LinearLayoutManager(App.context)
    }


    /**
     * ItemClickListener
     */
    private val ItemClickListener = BaseQuickAdapter.OnItemClickListener { _, _, position ->
    }


    /**
     * ItemChildClickListener
     */
    private val onItemChildClickListener = BaseQuickAdapter.OnItemChildClickListener { _, view, position ->
        when (view.id) {
        }
    }

    private fun initRecyclerView() {
        recyclerView.run {
            layoutManager = LayoutManager
            adapter = mAdapter
//            itemAnimator = DefaultItemAnimator()
//            recyclerViewItemDecoration.let { addItemDecoration(it) }
        }
        mAdapter.run {
            bindToRecyclerView(recyclerView)
            onItemChildClickListener = this@LoanHistoryActivity.onItemChildClickListener

//            setOnLoadMoreListener(onRequestLoadMoreListener, recyclerView)
            onItemClickListener = this@LoanHistoryActivity.ItemClickListener
            setEmptyView(R.layout.fragment_empty_layout)
        }
    }
}
